package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.atguigu.jxc.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Shine
 * @description：
 * @date ：2021/3/22 16:33
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSite = (page - 1) * rows;
        List<Supplier> supplierList = supplierDao.selectListBySupplierName(offSite, rows, supplierName);
        Integer total = 0;
        for (int i = 0; i < supplierList.size(); i++) {
            total++;
        }

        map.put("total", total);
        map.put("rows", supplierList);
        return map;
    }

    @Override
    public ServiceVO saveSupplierById(Supplier supplier) {
        if (supplier == null) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
        //    供应商添加或修改
        if (supplier.getSupplierId() != null) {
            supplierDao.updateSupplierById(supplier);
        } else {
            supplierDao.insertSupplierById(supplier);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO delete(String ids) {
        String[] split = ids.split(",");
        supplierDao.delete(split);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
}
